<?php

namespace Modules\Car\Jobs;

use App\Helpers\CommTrait;
use App\Http\Controllers\Miniprograme\SubscribeController;
use App\Http\Controllers\Miniprograme\TemplateController;
use App\Http\Controllers\Wx\SendTemplateController;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Modules\Car\Entities\RecognizeDrivingLicense;
use Modules\Car\Entities\Service;

class ServiceOverTimeJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels,CommTrait;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //进入维修
        // $this->repairing();
        //维修结束
        $this->repaired();
    }
    public function repairing(){
        // $create  = Service::query()
        //     ->where('need_start','>',now())
        //     ->where('need_end','<',now())
        //     ->whereNull('end')
        //     ->pluck('car_id')
        //     ->toArray();
        // if(count($create)>0){
        //     RecognizeDrivingLicense::query()
        //         ->whereIn('id',$create)
        //         ->update(['use_status'=>2]);
        // }
    }
    public function repaired(){
        $list = Service::query()
            ->where('need_end','>',now())
            ->whereNull('end')
            ->pluck('car_id')
            ->toArray();
        if(count($list)>0){
        Service::query()->where('need_end','>',now())->update(['end'=>now()]);
           foreach ($list as $item){
               if($item->is_custom==4){
                   $template = new SendTemplateController();
                   $managers = $this->getManager($item->department_id);
                   foreach ($managers as $openid){
                       info('事故维修完成通知'.$openid);
                       $template->serviceAccident($openid,$item);
                   }
//                   serviceAccident
                  // (new SubscribeController())->send_arrive($order,'接单通知');
                   // 完成维修进行统计
                   $this->serviceFinishedStatstatic($item);
               }
           }
        }
    }
}
